package com.android.projectsquirrel;

import java.util.ArrayList;
import java.util.List;

import android.app.*;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.database.Cursor;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TimePicker;
import android.widget.Toast;

public class survey_part2 extends Activity {
	String profilename;
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.survey_part2);
        String[] differentSiteArray = new String[] {"Yes","No","Select One"};
        String[]  foodlist = new String[] {"Bird Feeders","Human Handouts","Garbage","Trees and other plants","Other","Select One"};
        String[]  numberlist = new String[] {"Stayed the same","Increased","Decreased","Select One"};
        
        Spinner food_spinner = (Spinner) findViewById(R.id.foodSpinner);
        Spinner number_spinner = (Spinner) findViewById(R.id.numberSpinner);
        ArrayAdapter<String> number_adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item, numberlist);
    number_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    number_spinner.setAdapter(number_adapter);
    number_spinner.setSelection(3);
        
        ArrayAdapter<String> food_adapter = new ArrayAdapter<String>(this,
                    android.R.layout.simple_spinner_item, foodlist);
        food_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        food_spinner.setAdapter(food_adapter);
        food_spinner.setSelection(5);
        
        Spinner differentSiteSpinner = (Spinner)findViewById(R.id.differentSiteSpinner);
        ArrayAdapter<String> differentSite_adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, differentSiteArray);
        differentSite_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        differentSiteSpinner.setAdapter(differentSite_adapter);
        differentSiteSpinner.setSelection(2);
        
        Bundle extras = getIntent().getExtras();
    	profilename = extras.getString("profilename");
     
        
    
    Button submitButton = (Button) findViewById(R.id.Button01);
    submitButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
        	Boolean okToProceed = checkForErrorsPart2();
        	if(okToProceed){
        		if(checkIfExists()){
        			update();
        		}
        		else{
        			insertData2();
        		}
        		logResults();
        	}
        }
    });
	}
	
	public void update(){
		DBAdapter_Survey2 dba = new DBAdapter_Survey2(this);
		DatePicker datePicker  = (DatePicker)findViewById(R.id.DatePicker01);
    	TimePicker timePicker = (TimePicker)findViewById(R.id.TimePicker01);
    	EditText editEmail = (EditText)findViewById(R.id.email);
    	EditText editGrayNumber = (EditText)findViewById(R.id.graynumber);
    	EditText editFoxNumber = (EditText)findViewById(R.id.foxnumber);
    	Spinner foodSource = (Spinner)findViewById(R.id.foodSpinner);
    	EditText otherFood = (EditText)findViewById(R.id.foodother);
    	Spinner compareSpinner = (Spinner)findViewById(R.id.numberSpinner);
    	Spinner diffSpinner = (Spinner)findViewById(R.id.differentSiteSpinner);
    	EditText editComments = (EditText)findViewById(R.id.comments);
		 dba.open();
		 dba.updateTitle2(profilename,
			 			Integer.toString(datePicker.getDayOfMonth()),
			 			Integer.toString(datePicker.getMonth()),
			 			Integer.toString(datePicker.getYear()),
			 			Integer.toString(timePicker.getCurrentHour()),
			 			Integer.toString(timePicker.getCurrentMinute()),
//			 			Integer.toString(timePicker.get),//	
			 			"am",
			 			editEmail.getText().toString(),
			 			editGrayNumber.getText().toString(),
			 			editFoxNumber.getText().toString(),
		        		foodSource.getSelectedItemPosition(),
		        		otherFood.getText().toString(),
		        		compareSpinner.getSelectedItemPosition(),
		        		diffSpinner.getSelectedItemPosition(),
		        		editComments.getText().toString()
		        		);
		        		        
		        dba.close();
	}
	
	public Boolean checkIfExists(){
		DBAdapter_Survey2 dba = new DBAdapter_Survey2(this);
		 dba.open();
		 Boolean flag;
		 flag = dba.checkProfile(profilename);
		 return flag;
		 
	}
	public void show(String st){
		Toast t = Toast.makeText(this,st, Toast.LENGTH_LONG);
    	t.show();
	}
	
	public void insertData2(){
	    	DBAdapter_Survey2 dba = new DBAdapter_Survey2(this);
	    	DatePicker datePicker  = (DatePicker)findViewById(R.id.DatePicker01);
	    	TimePicker timePicker = (TimePicker)findViewById(R.id.TimePicker01);
	    	EditText editEmail = (EditText)findViewById(R.id.email);
	    	EditText editGrayNumber = (EditText)findViewById(R.id.graynumber);
	    	EditText editFoxNumber = (EditText)findViewById(R.id.foxnumber);
	    	Spinner foodSource = (Spinner)findViewById(R.id.foodSpinner);
	    	EditText otherFood = (EditText)findViewById(R.id.foodother);
	    	Spinner compareSpinner = (Spinner)findViewById(R.id.numberSpinner);
	    	Spinner diffSpinner = (Spinner)findViewById(R.id.differentSiteSpinner);
	    	EditText editComments = (EditText)findViewById(R.id.comments);
	    	
			 dba.open();
			 Boolean id;
			 long ins;
			 	ins = dba.insertTitle2(profilename,
			 			Integer.toString(datePicker.getDayOfMonth()),
			 			Integer.toString(datePicker.getMonth()),
			 			Integer.toString(datePicker.getYear()),
			 			Integer.toString(timePicker.getCurrentHour()),
			 			Integer.toString(timePicker.getCurrentMinute()),
//			 			Integer.toString(timePicker.get),//	
			 			"am",
			 			editEmail.getText().toString(),
			 			editGrayNumber.getText().toString(),
			 			editFoxNumber.getText().toString(),
		        		foodSource.getSelectedItemPosition(),
		        		otherFood.getText().toString(),
		        		compareSpinner.getSelectedItemPosition(),
		        		diffSpinner.getSelectedItemPosition(),
		        		editComments.getText().toString()
		        		);
		        		        
		        dba.close();
		}
	
	public void logResults(){
	    	Context context = getApplicationContext();
	    	String profiles = new String();
	    	DBAdapter_Survey2 db = new DBAdapter_Survey2(this);
	    	db.open();
	    	Cursor c = db.getTitle2(profilename);
	    	startManagingCursor(c);
	    	if(c.moveToFirst()){
	    				 profiles = profiles.concat(c.getString(0)+c.getString(1)+c.getString(2));
	    		       	    c.moveToNext();
//	    		        }
	    	c.close();
	    	db.close();

	    	
	    	}

		}
	
	public Boolean checkForErrorsPart2(){
		Boolean ok = true;
		String questionNumbers = "Please answer the following survey questions: \n";
		EditText checkGraySquirrels = (EditText) findViewById(R.id.graynumber);
		if((checkGraySquirrels.getText().toString()).equals("")){
			questionNumbers = questionNumbers.concat("Question 4.Please enter the number of gray squirrels.\n");
			ok = false;
			
		}
		EditText checkfoxSquirrels = (EditText) findViewById(R.id.foxnumber);
		if((checkfoxSquirrels.getText().toString()).equals("")){
			questionNumbers = questionNumbers.concat("Question 4.Please enter the number of fox squirrels.\n");
			ok = false;
			
		}
		Spinner checkFoodSources = (Spinner) findViewById(R.id.foodSpinner);
		if(checkFoodSources.getSelectedItemPosition()== 5){
			questionNumbers = questionNumbers.concat("Question 5. Please select a squirrel environment.\n");
			ok = false;
			
		}
		Spinner checkCompare = (Spinner) findViewById(R.id.numberSpinner);
		if(checkCompare.getSelectedItemPosition()== 5){
			questionNumbers = questionNumbers.concat("Question 6. Please select a value for the squirrel numbers when compared to last year.\n");
			ok = false;
			
		}
		Spinner checkDifferentSite = (Spinner) findViewById(R.id.differentSiteSpinner);
		if(checkDifferentSite.getSelectedItemPosition()== 3){
			questionNumbers = questionNumbers.concat("Question 7. Please select 'Yes' or 'No' based on your previous submissions.\n");
			ok = false;
			
		}
		if(ok == false){
			showAlert(questionNumbers);
		}
		return ok;
	}
	
	public void showAlert(String str){
		 // prepare the alert box
       AlertDialog.Builder alertbox = new AlertDialog.Builder(this);

       // set the message to display
       alertbox.setMessage(str);

       // add a neutral button to the alert box and assign a click listener
       alertbox.setNegativeButton("Ok", new DialogInterface.OnClickListener() {

           // click listener on the alert box
           public void onClick(DialogInterface arg0, int arg1) {
           }
       });

       // show it
       alertbox.show();
	}
	
	 public boolean onCreateOptionsMenu(Menu menu) {
	        boolean supRetVal = super.onCreateOptionsMenu(menu);
	        menu.add(Menu.NONE, 0, Menu.NONE, "Go Home");
	    	//.setIcon(android.R.drawable.);
		return supRetVal;
	}
	
	
	public boolean onOptionsItemSelected(MenuItem item) {
	    switch (item.getItemId()) {
	        case 0:
	        	startActivity(new Intent(survey_part2.this, Main.class));
	            return true;
	    }
	    return false;
	}

	}

